home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_016 / source.files / showilbm.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  268 lines

  1. /** ShowILBM.c **************************************************************
  2.  *
  3.  * Read an ILBM raster image file and display it.      24-Jan-86.
  4.  *
  5.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  9.  *
  10.  * The IFF reader portion is essentially a recursive-descent parser.
  11.  * The display portion is specific to the Commodore Amiga computer.
  12.  *
  13.  * NOTE: This program displays an image, pauses, then exits.
  14.  *
  15.  * Usage from CLI:
  16.  *   showilbm picture1 [picture2] ...
  17.  *
  18.  * Usage from WorkBench:
  19.  * Click on ShowILBM, hold down shift key, click on each picture to show,
  20.  * Double-click on final picture to complete the selection, release the
  21.  * shift key.
  22.  *
  23.  ****************************************************************************/
  24.  
  25. /* If you are constructing a Makefile, here are the names of the files 
  26.  * that you'll need to compile and link with to use showilbm:
  27.  
  28.     showilbm.c
  29.     readpict.c
  30.     remalloc.c
  31.     ilbmr.c
  32.     iffr.c
  33.     unpacker.c
  34.     gio.c
  35.  
  36.     and you'll have to get movmem() from lc.lib
  37.  
  38.  * robp.
  39.  * ********************************************************************** */
  40.  
  41.  
  42. #include "iff/intuall.h"
  43. #include "libraries/dos.h"
  44. #include "libraries/dosextens.h"
  45. #include "iff/ilbm.h"
  46. #include "workbench/workbench.h"
  47. #include "workbench/startup.h"
  48. #include "iff/readpict.h"
  49. #include "iff/remalloc.h"
  50.  
  51. #define LOCAL static
  52.  
  53. #define MIN(a,b) ((a)<(b)?(a):(b))
  54. #define MAX(a,b) ((a)>(b)?(a):(b))
  55.  
  56. /* general usage pointers */
  57. struct GfxBase *GfxBase;
  58. LONG IconBase;    /* Actually, "struct IconBase *" if you've got some ".h" file*/
  59.  
  60. /* For displaying an image */
  61. LOCAL struct RastPort rP;
  62. LOCAL struct BitMap bitmap0;
  63. LOCAL struct RasInfo rasinfo;
  64. LOCAL struct View v = {0};
  65. LOCAL struct ViewPort vp = {0};
  66.  
  67. LOCAL ILBMFrame iFrame;
  68.     
  69. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  70. #define bufSz 512
  71.  
  72. /* Message strings for IFFP codes. */
  73. LOCAL char MsgOkay[]        = {
  74.     "(IFF_OKAY) Didn't find a FORM ILBM in the file." };
  75. LOCAL char MsgEndMark[]     = { "(END_MARK) How did you get this message?" };
  76. LOCAL char MsgDone[]        = { "(IFF_DONE) All done."};
  77. LOCAL char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." };
  78. LOCAL char MsgNot[]         = { "(NOT_IFF) Not an IFF file." };
  79. LOCAL char MsgNoFile[]      = { "(NO_FILE) No such file found." };
  80. LOCAL char MsgClientError[] = {
  81.     "(CLIENT_ERROR) ShowILBM bug or insufficient RAM."};
  82. LOCAL char MsgForm[]        = { "(BAD_FORM) A malformed FORM ILBM." };
  83. LOCAL char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM ILBM." };
  84. LOCAL char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." };
  85.  
  86. /* THESE MUST APPEAR IN RIGHT ORDER!! */
  87. LOCAL char *IFFPMessages[-(int)LAST_ERROR+1] = {
  88.     /*IFF_OKAY*/  MsgOkay,
  89.     /*END_MARK*/  MsgEndMark,
  90.     /*IFF_DONE*/  MsgDone,
  91.     /*DOS_ERROR*/ MsgDos,
  92.     /*NOT_IFF*/   MsgNot,
  93.     /*NO_FILE*/   MsgNoFile,
  94.     /*CLIENT_ERROR*/ MsgClientError,
  95.     /*BAD_FORM*/  MsgForm,
  96.     /*SHORT_CHUNK*/  MsgShort,
  97.     /*BAD_IFF*/   MsgBad
  98.     };
  99.  
  100. /** DisplayPic() ************************************************************
  101.  *
  102.  * Interface to Amiga graphics ROM routines.
  103.  *
  104.  ****************************************************************************/
  105. DisplayPic(bm, ptilbmFrame)
  106.     struct BitMap *bm;  ILBMFrame *ptilbmFrame;  {
  107.     int i;
  108.     struct View *oldView = GfxBase->ActiView;    /* so we can restore it */
  109.  
  110.     InitView(&v);
  111.     InitVPort(&vp);
  112.     v.ViewPort = &vp;
  113.     InitRastPort(&rP);
  114.     rP.BitMap = bm;
  115.     rasinfo.BitMap = bm;
  116.  
  117.     /* Always show the upper left-hand corner of this picture. */
  118.     rasinfo.RxOffset = 0;
  119.     rasinfo.RyOffset = 0;
  120.  
  121.     vp.DWidth = MAX(ptilbmFrame->bmHdr.w, 4*8);
  122.     vp.DHeight = ptilbmFrame->bmHdr.h;
  123.  
  124. #if 0
  125.     /* Specify where on screen to put the ViewPort. */
  126.     vp.DxOffset = ptilbmFrame->bmHdr.x;
  127.     vp.DyOffset = ptilbmFrame->bmHdr.y;
  128. #else
  129.     /* Always display it in upper left corner of screen.*/
  130. #endif
  131.  
  132.     if (ptilbmFrame->bmHdr.pageWidth <= 320) 
  133.     vp.Modes = 0;
  134.     else vp.Modes = HIRES;
  135.     if (ptilbmFrame->bmHdr.pageHeight > 200) {
  136.     v.Modes |= LACE;
  137.     vp.Modes |= LACE;
  138.     }
  139.     vp.RasInfo = &rasinfo;
  140.     MakeVPort(&v,&vp);
  141.     MrgCop(&v);
  142.     LoadView(&v);    /* show the picture */
  143.     WaitBlit();
  144.     WaitTOF();
  145.     LoadRGB4(&vp, ptilbmFrame->colorMap, ptilbmFrame->nColorRegs);
  146.  
  147.     for (i = 0; i < 5*60; ++i)  WaitTOF();    /* Delay 5 seconds. */
  148.  
  149.     LoadView(oldView);    /* switch back to old view */
  150.     }
  151.  
  152. /** stuff for main0() *******************************************************/
  153. LOCAL struct WBStartup *wbStartup = 0;    /* 0 unless started from WorkBench.*/
  154.  
  155. PrintS(msg)  char *msg; {
  156.     if (!wbStartup)  printf(msg);
  157.     }
  158.  
  159. void GoodBye(msg)  char *msg; {
  160. /*     PrintS(msg);   PrintS("\n");  */
  161.     printf(msg);   printf("\n");    /* If linked with Lstartup.obj and
  162.                      * NOT compiled with -dTINY, this 
  163.                      * outputs the message to the window
  164.                      * that Lattice opens.
  165.                      * ... carolyn.
  166.                      */
  167.     exit(0);
  168.     }
  169.  
  170. /** OpenArg() ***************************************************************
  171.  *  Given a "workbench argument" (a file reference) and an I/O mode.
  172.  *  It opens the file.
  173.  ****************************************************************************/
  174. LONG OpenArg(wa, openmode)  struct WBArg *wa;   int openmode; {
  175.     LONG olddir;
  176.     LONG file;
  177.     if (wa->wa_Lock)   olddir = CurrentDir(wa->wa_Lock);
  178.     file = Open(wa->wa_Name, openmode);
  179.     if (wa->wa_Lock)   CurrentDir(olddir);
  180.     return(file);
  181.     }
  182.  
  183. /** main0() *****************************************************************/
  184. void main0(wa)  struct WBArg *wa;  {
  185.     LONG file;
  186.     IFFP iffp = NO_FILE;
  187.  
  188.     /* load and display the picture */
  189.     file = OpenArg(wa, MODE_OLDFILE);
  190.     if (file)
  191.     iffp = ReadPicture(file, &bitmap0, &iFrame, ChipAlloc);
  192.     /* Allocates BitMap using ChipAlloc().*/
  193.     Close(file);
  194.     if (iffp == IFF_DONE)
  195.     DisplayPic(&bitmap0, &iFrame);
  196.  
  197. /*     PrintS(" ");   PrintS(IFFPMessages[-iffp]);   PrintS("\n");   */
  198.      printf(" ");   printf(IFFPMessages[-iffp]);   printf("\n");  
  199.     /* see note near definition of PrintS */
  200.  
  201.     /* cleanup */
  202.     if (bitmap0.Planes[0])  {
  203.     RemFree(bitmap0.Planes[0]);
  204.         /* ASSUMES allocated all planes via a single ChipAlloc call.*/
  205.     FreeVPortCopLists(&vp);
  206.     FreeCprList(v.LOFCprList);
  207.     }
  208.     }
  209.  
  210. extern struct WBStartup *WBenchMsg;    /* added: Carolyn Scheppner */
  211.  
  212. /** main() ******************************************************************/
  213.  
  214. void main(argc, argv)  int argc;  char **argv;  {
  215.     struct WBArg wbArg, *wbArgs;
  216.     LONG olddir;
  217. /*sss    struct Process *myProcess; */
  218.  
  219.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  220.     GoodBye("No graphics.library");
  221.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  222.     GoodBye("No icon.library");
  223.     if (!argc) {
  224.     /* Invoked via workbench */
  225. /*     wbStartup = (struct WBStartup *)argv;   */
  226.     wbStartup = WBenchMsg;    /* modified by Carolyn Scheppner */
  227.     wbArgs = wbStartup->sm_ArgList;
  228.     argc = wbStartup->sm_NumArgs;
  229.     while (argc >= 2) {
  230.         olddir = CurrentDir(wbArgs[1].wa_Lock);
  231.         main0(&wbArgs[1]);
  232.         argc--;   wbArgs = &wbArgs[1];
  233.         }
  234. #if 0
  235.     /* [TBD] We want to get an error msg to the Workbench user... */
  236.     if (argc < 2) {
  237.         printf ("Usage from workbench:\n");
  238.         printf (" Click mouse on Show-ILBM, Then hold 'SHIFT' key\n");
  239.  
  240.     /* BOTH OF THESE WERE "PrintS", see note near PrintS definition */
  241.  
  242.         GoodBye(" while double-click on file to display.");
  243.         }
  244. #endif
  245.     }
  246.     else {
  247.     /* Invoked via CLI.  Make a lock for current directory.
  248.      * Eventually, scan name, separate out directory reference?*/
  249.     if (argc < 2)
  250.         GoodBye("Usage from CLI: 'Show-ILBM filename'");
  251. /*sss    myProcess = (struct Process *)FindTask(0); */
  252.     wbArg.wa_Lock = 0; /*sss myProcess->pr_CurrentDir; */
  253.     while (argc >= 2) {
  254.         wbArg.wa_Name = argv[1];
  255.         printf("Showing file ");   printf(wbArg.wa_Name);   printf(" ...");
  256.     /* THESE WERE "PrintS", see note near PrintS definition */
  257.         main0(&wbArg);
  258.         printf("\n");
  259.     /* THIS WAS "PrintS", see note near PrintS definition */
  260.         argc--;   argv = &argv[1];
  261.         }
  262.     }
  263.     CloseLibrary(GfxBase);
  264.     CloseLibrary(IconBase);
  265.     exit(0);
  266.     }
  267.  
  268.